package com.itheima.controller;

import com.itheima.entity.ValidateNote;
import com.itheima.errors.BusinessException;
import com.itheima.utils.ValidationUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;



@Aspect
@Component
public class ResultResponseAspect {
    @Pointcut("execution(public * com.itheima.controller.*.*(..))")
    public void webPointCut(){

    }

    @Before(value = "webPointCut()")
    public void before(JoinPoint pjp) throws Throwable{

        Object[] args = pjp.getArgs();
        System.out.println(pjp.toString());
        System.out.println("------------------");
        for (Object arg : args) {
            System.out.println(arg);
        }
        for (Object arg : args) {
            if (arg instanceof ValidateNote){

                ValidationUtil.ValidResult validResult = ValidationUtil.validateBean(arg);
                if (validResult.hasErrors()){
                    throw new BusinessException(validResult.getErrors());
                }

            }
        }
    }
}
